<?php

class Admini_TodoTaskController extends Uuch_ManageController {

      public function init() {
            $this->appendCss('/css/home/warehouse.css');
            $this->appendCss('/admins/css/common.css');
            parent::init();
            //        $this->appendCss('/css/home/warehouse.css');
            //        $this->appendCss('/admins/css/common.css');
            $this->appendCss('/admins/css/yundan.css');
            $this->appendScript('/admins/js/jquery/jquery-1.7.2.min.js')->appendScript('/js/jquery.validate.min.js')->appendScript('/js/messages_cn.js')->appendScript('/js/uuch.js');
            $this->view->type = "todotask";
      }

      public function addAction() {
            $objectId = $this->getSafeParam('objectId');
            $objectType = $this->getSafeParam('objectType');
            if ($objectType) {
                  $DomainobjectType = Application_Model_DomainobjectType::getObjectItem($objectType);
                  $name = $DomainobjectType->name;
                  $this->view->name = $name;
            }
            if (!$objectId) {
                  $this->setTitle('添加待办事项');
            } else {
                  $title = '添加' . $name . '待办事项';
                  $this->setTitle($title);
            }
            if($objectId&&$objectType){
                  $this->disableLayout();
            }
            $this->view->objectId = $objectId;
            $this->view->objectType = $objectType;
            $this->view->objlist = Application_Model_TodoTask::byUserBlockGetAllByObjectTypeId($objectType, $objectId);
            $this->view->objtype = Application_Model_DomainobjectType::getAll();
            $this->view->worker = Application_Model_adminuser::getAll();
            $this->view->conse = 0;
            if (!$this->isPost()) {
                  return;
            }
            //选取字段，记录到代办事项列表
            $userItem = new Application_Model_TodoTask();
            $userItem->id = $this->getSafeParam("addid");
            $userItem->objectType = $this->getSafeParam("objectType");
            $userItem->objectId = $this->getSafeParam("objectid");
            $userItem->dateCreated = $this->now();
            $userItem->expectTime = $this->getSafeParam("starttime") ? $this->getSafeParam("starttime") : date('Y-m-d');
            $userItem->priority = $this->getSafeParam("priority");
            $userItem->title = $this->getSafeParam("title");
            $userItem->status = $this->getSafeParam('objstatus');
            $userItem->creator = Uuch_Util::getUserNikeName();
            $userItem->operator = $this->getSafeParam('distribut');
            $userItem->content = $this->getSafeParam("content");
            $userItem->save();
            //产生事项添加记录
            if ($this->getSafeParam("addid")) {
                  $content = '修改待办事项';
                  $operateType = $this->getSafeParam('objstatus');
            } else {
                  $content = '添加待办事项';
                  $operateType = 1;
            }
            $trackobjectId = $userItem->id;
            $trackobjectType = 3;

            $operator = Uuch_Util::getUserNikeName();
            $dateAdd = $this->now();
            $isUserVisible = 0;
            Application_Model_Tracking::addTrack($trackobjectType, $trackobjectId, $operator, $content, $isUserVisible, $operateType, $dateAdd);
            $this->writeLog(0, '', '待办事项id='.$userItem->id, '添加待办事项', $operator);
            $this->view->conse = 1;
      }

      public function indexAction() {
            $this->setTitle('待办事项列表');
//            $name = $this->getSafeParam("objectId");
            $page = $this->getSafeParam("pagesize");
            $pagesize = $page ? $page : 10;
            $pageindex = $this->getSafeParam('start');
            if (!$pageindex) {
                  $pageindex = 0;
            }
            $quick = $this->getSafeParam("quick");
            $title = $this->getSafeParam("title");
            $timescope = $this->getSafeParam('timescope');
            $starttime = $this->getSafeParam("starttime");
            $endtime = $this->getSafeParam("endtime");
            $searchType = $this->getSafeParam("searchType");
            $creator = $this->getSafeParam("creator");
            $strart = date("m") % 3 == 0 ? date("m") / 3 : ((int) (date("m") / 3) + 1);
            if ($timescope != null) {
                  switch ($timescope) {
                        case "today":
                              $starttime = date("Y-m-d 00:00:00", mktime(23, 59, 59, date("m"), date("d"), date("Y")));
                              $endtime = date("Y-m-d", mktime(23, 59, 59, date("m"), date("d"), date("Y")));
                              break;
                        case "week":
                              $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 6, date("Y")));
                              $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
                              break;
                        case "month":
                              $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), 1, date("Y")));
                              $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
                              break;
                        case "threemonth":
                              $starttime = date("Y-m-d", mktime(0, 0, 0, ($strart - 1) * 3 + 1, 1, date("Y")));
                              $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
                              break;
                        case "year":
                              $starttime = date("Y-m-d", mktime(0, 0, 0, 1, 1, date("Y")));
                              $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
                              break;
                        default :
                              $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 90, date("Y")));
                              $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
                              break;
                  }
            }
            $objectSearch = new Application_Model_Filter_TodoTaskSearch();
            $objectSearch->quick = $quick;
            $objectSearch->startTime = $starttime;
            $objectSearch->endTime = $endtime;
            $objectSearch->creator = $creator;
            $objectSearch->nametitle = $title;
            $objectSearch->status = $searchType ? $searchType : "";
            $result = Application_Model_TodoTask::getFilter($objectSearch, $pagesize, $pageindex);
            $this->view->objlist = $result['list'];
            $this->view->objAll = $result;
            $this->view->worker = Application_Model_adminuser::getAll();
            $this->view->objtype = Application_Model_DomainobjectType::getAll();
            $this->view->list = '/admini/todotask/index/';
            $status = $this->getSafeParam('status');
            $distribut = $this->getSafeParam("objoperator");
            $this->view->quick = $quick;
            $this->view->timescope = $timescope;
            $this->view->starttime = $starttime;
            $this->view->endtime = $endtime;
            $this->view->status = $searchType;
            $this->view->stype = 1;
            if ($distribut) {
                  $id = $this->getSafeParam("objid");
                  $Item = Application_Model_TodoTask::load($id);
                  $Item->operator = $distribut;
                  $Item->save();
                  $objectId = $id;
                  $operateType = $Item->status;
                  $operator = $distribut;
                  $content = "指派给" . $distribut;
                  $dateAdd = $this->now();
                  $isUserVisible = 0;
                  $objectType = 3;
                  Application_Model_Tracking::addTrack($objectType, $objectId, $operator, $content, $isUserVisible, $operateType, $dateAdd);
                  $this->writeLog(0, '', '待办事项id='.$id, '将待办事项指派给'.$distribut, $operator);
                  $this->json(1);
                  exit;
            }
            if ($status) {
                  $objecttrack = Application_Model_TodoTask::load($status);
                  $objecttrack->id = $status;
                  $objecttrack->status++;
                  $objecttrack->save();
                  $objectId = $objecttrack->id;
                  $objectType = 3;
                  $operateType = $objecttrack->status;
                  $operator = Uuch_Util::getUserNikeName();
                  switch ($operateType) {
                        case 2:
                              $content = $operator . '确定开始';
                              break;
                        case 3:
                              $content = $operator . '确定完成';
                        default:
                              break;
                  }
                  $dateAdd = $this->now();
                  $isUserVisible = 0;
                  Application_Model_Tracking::addTrack($objectType, $objectId, $operator, $content, $isUserVisible, $operateType, $dateAdd);
                  $this->writeLog(0, '', '待办事项id='.$objectId, $content, $operator);
                  $this->redirect('index');
            }
      }

      public function detailAction() {
            $this->setTitle('代办事项详情');
            $objectId = $this->getSafeParam("id");
            $objresult = Application_Model_TodoTask::load($objectId);
            $this->view->objresult = $objresult;
            $tracklistobjectType = 3;
            $trackItem = Application_Model_Tracking::getAllByObjectTypeId($tracklistobjectType, $objectId);
            $this->view->tracklist = $trackItem;
      }
      
      public function trackAction() {
            $this->disableLayout();
            $objectId = $this->getSafeParam('objectId');
            $trackItem = Application_Model_Tracking::getTrackbyobjectId($objectId);
            $this->view->tracklist = $trackItem;
      }

      public function editAction() {
            $this->setTitle('修改待办事项');
            $objectId = $this->getSafeParam("id");
            $objresult = Application_Model_TodoTask::load($objectId);
            $this->view->objresult = $objresult;
            $tracklistobjectType = 3;
            $trackItem = Application_Model_Tracking::getAllByObjectTypeId($tracklistobjectType, $objectId);
            $this->view->tracklist = $trackItem;
            $this->view->objtype = Application_Model_DomainobjectType::getAll();
            if (!$this->isPost()) {
                  return;
            }
            //选取字段，记录到代办事项列表
            if ($this->getSafeParam("addid")) {
                  $objresult->objectType = $this->getSafeParam("objectType");
                  $objresult->objectId = $this->getSafeParam("objectid");
                  $objresult->expectTime = $this->getSafeParam("starttime");
                  $objresult->priority = $this->getSafeParam("priority");
                  $objresult->title = $this->getSafeParam("title");
                  $objresult->status = $this->getSafeParam("objstatus");
                  $objresult->content = $this->getSafeParam("content");
                  $objresult->save();
                  //产生事项添加记录
                  $trackobjectId = $objresult->id;
                  $trackobjectType = 3;
                  $operateType = $objresult->status;
                  $operator = Uuch_Util::getUserNikeName();
                  $content = '修改待办事项';
                  $dateAdd = $this->now();
                  $isUserVisible = 0;
                  $this->writeLog(0, '', '待办事项id='.$this->getSafeParam("addid"), '修改待办事项', $operator);
                  Application_Model_Tracking::addTrack($trackobjectType, $trackobjectId, $operator, $content, $isUserVisible, $operateType, $dateAdd);
                  $this->view->retrack = 1;
            }
      }

      public function deleAction() {
            $this->view->ma = 0;
            echo $id = $this->getSafeParam('id');
            if ($id) {
                  $result = Application_Model_Tracking::getTrackbyobjectId($id);
                  foreach ($result as $value) {
                        $value->delete();
                  }
                  $task = Application_Model_TodoTask::load($id);
                  $task->delete();
                  $money = 0;
                  $type ='';
                  $reid = '待办事项列表中id为'.$id;
                  $content ='对id='.$id. '的待办事项执行删除操作';
                  $userid =  Uuch_Util::getUserId();
                  $this->writeLog($money, $type, $reid, $content, $userid);
                  $this->redirect('index');
            }
           
            /**if ($idlist) {
                  $arrid = explode(",", $idlist);
                  for ($i = 0; $i < count($arrid); $i++) {
                        $userItem = Application_Model_Job::load($arrid[$i]);
                        if ($userItem) {
                              $userItem->delete();
                        }
                  }
            }*/
      }
      public function checkAction(){
            $objectId = $this->getSafeParam('objectId');
            $objectType = $this->getSafeParam('objectType');
            $checkItem = Application_Model_DomainobjectType::getIssetobjectId($objectType, $objectId);
            if($checkItem){
                 $this->json(1);
                 return ;
            }
            $this->json(0);
      }

}
